using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Xml;
using CodePlex.SpaceBlock;
using System.Reflection;
using System.Diagnostics;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;


namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class MainForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {

        private readonly ExploreControl _explorer;
        public MainForm()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            UIManager.Instance = new UIManager(_smallIcons,_statusBarLabel);

            _explorer = new ExploreControl();

            AuthUtils.FileSystemManagerChanged += delegate
            {
                this.InDisplay(delegate
                {
                    if (AuthUtils.FileSystemManager.IsValid)
                    {
                        UIManager.Instance.LogForm.Initialize(AuthUtils.FileSystemManager);
                        _explorer.Initialize(AuthUtils.FileSystemManager);
                    }
                });
            };

            if (!AuthUtils.AppStartup(this))
            {
                this.Close();
            }
        }

        public bool ShowToolsOptions(bool zombieMode)
        {
            OptionsForm dialog = new OptionsForm();
            dialog.Icon = UIManager.Instance.GetIcon("Options");
            dialog.Initialize(this, AuthUtils.FileSystemManager, zombieMode);
            dialog.StartPosition = FormStartPosition.CenterScreen;
            DialogResult rt = dialog.ShowDialog(this);

            if (rt == DialogResult.Cancel && !AuthUtils.FileSystemManager.IsValid)
            {
                this.Close();
                return false;
            }

            return true;
        }


     






        private void MainForm_Load(object sender, EventArgs e)
        {
            if (AuthUtils.FileSystemManager == null || !AuthUtils.FileSystemManager.IsValid)
            {
                return;
            }
            this.Text = string.Format(Properties.Messages.MainForm_Title,
                UIConstants.ApplicationName,
                UIConstants.ApplicationVersion);

            _menu.ImageList = UIManager.Instance.SmallImageList;


            _exitMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _exitMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("Exit");
            _exitMenuItem.Click += delegate
            {
                Application.Exit();
            };

            _logWindowMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _logWindowMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("ShowLog");
            _logWindowMenuItem.Click += delegate
            {
                UIManager.Instance.LogForm.Show();
                UIManager.Instance.LogForm.Activate();
            };


            _aboutSpaceBlockMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _aboutSpaceBlockMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("About");
            _aboutSpaceBlockMenuItem.Click += delegate
            {
                Process.Start(Properties.Messages.Urls_About_SpaceBlock);
            };

            _aboutResourcefulMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _aboutResourcefulMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("About");
            _aboutResourcefulMenuItem.Click += delegate
            {
                Process.Start(Properties.Messages.Urls_About_Resourceful);
            };

            _resetMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _resetMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("Key");
            _resetMenuItem.Click += delegate
            {
                AuthUtils.ResetMasterPassword(this, true,false);
            };

            _optionsMenuItem.GetCurrentParent().ImageList = UIManager.Instance.SmallImageList;
            _optionsMenuItem.ImageIndex = UIManager.Instance.GetSmallImageListIndex("Options");
            _optionsMenuItem.Click += delegate
            {
                ShowToolsOptions(false);
            };

            UIUtils.FillWith(toolStripContainer1.ContentPanel, _explorer);
            _exploreButton.Click += delegate
            {
                _explorer.Visible = true;
                _exploreButton.Checked = true;

            };
            _exploreButton.PerformClick();


            UIManager.Instance.LogForm.Initialize(AuthUtils.FileSystemManager);
            _explorer.Initialize(AuthUtils.FileSystemManager);

            UIUtils.MinimizeToTray(this,UIManager.Instance.GetIcon("Abstract"));

        }
    }
}

